<%@ Language=VBScript %>
<script language=vbscript runat=server>
Function LayIDCB(bStr )
    bStr = Trim(bStr)
    Dim bSo 
    bSo = InStr(1, bStr, "_ID", vbTextCompare)
    If bSo Then
        LayIDCB = Mid(bStr, bSo + 3)
    End If
End Function
</script>
<%	
	set Conn=server.CreateObject("ADODB.Connection")
	Conn.Open application("StrConn")
	
	nodeid=Request("id")
	'userid=request("user")
	userid=LayIDCB(request("canbo"))
	IDFlow=request("IDFlow")
	Yeucau=Request("Yeucau")
	ykien=Request("ykien")
	thoihan=Request("txtthoihan")
	
	'*********Gui theo nhom************
	bNhom=Request("bNhom")
	bMaNhom=Request("nhom")
	bluong=LayIDCB(Request("luong"))
select case bNhom
	case "nhom"	
		on error resume next
		Conn.BeginTrans
		sSQL="Select * from Canbo_nhom Where Manhom='"& bMaNhom &"'"		
		Set RecNhom=server.CreateObject("ADODB.Recordset")
		RecNhom.CursorType=3
		RecNhom.CacheSize=5
		RecNhom.Open sSQL,Conn
		bMaFlownode=nodeid
		bnum=RecNhom.RecordCount
	if CheckNumberNode(bMaflownode,99,bnum) then 'Kiem tra so nhanh o mot node voi muc nhanh cho phep
		Dim dau,cuoi,vitri,cat
		Do While not RecNhom.EOF
			userid=RecNhom("Macanbo")
			sql1="Insert Into TvFlowNode(Maworkflownode,Maworkflow,nguoixuly,yeucauxuly,ktuser) values('" & bMaFlownode & "','" & IDFlow & "','" & userid & "','"& Yeucau &"','1')"
			conn.Execute sql1
			vitri=ITemlast(bMaFlownode)*2+3
			dau=left(bMaFlownode,vitri)
			cuoi=right(bMaFlownode,len(bMaFlownode)-vitri)
			cat=cint(right(dau,2))
			if cat<9 then 
				bTang="0" & cstr(cat+1)
			else
				bTang=cstr(cat+1)
			end if
			bMaFlownode=left(dau,len(dau)-2) & bTang & cuoi
			RecNhom.MoveNext
		Loop
		RecNhom.Close
		Set RecNhom=nothing
		bThongbao=false
	else
		bThongbao=true
	end if
		if Err.number then
			Conn.RollbackTrans
		else
			Conn.CommitTrans
		end if
		Err.Clear
	case "luong"
	if CheckGepNode(nodeid,bluong) then
		
		dim mnode(),mnxl(),myc()
		dim i
		sSQL="Select maworkflownode,yeucauxuly,nguoixuly from tvflownode Where maworkflow like '"& bluong &"' order by maworkflownode"		
		Set RecLuong=server.CreateObject("ADODB.Recordset")
		RecLuong.CursorType=3
		RecLuong.CacheSize=5
		RecLuong.Open sSQL,Conn
		redim mnode(RecLuong.RecordCount)
		redim mnxl(RecLuong.RecordCount)
		redim myc(RecLuong.RecordCount)
		i=1
		do while not RecLuong.EOF
			mnode(i)=left(GepNode(nodeid,RecLuong("maworkflownode")),23)
			mnxl(i)=RecLuong("nguoixuly")
			myc(i)=RecLuong("yeucauxuly")
			RecLuong.MoveNext
			i=i+1						
		loop		
		RecLuong.Close
		Set RecLuong=nothing
		for j=1  to i-1
			sqll="Insert Into TvFlowNode(Maworkflownode,Maworkflow,nguoixuly,yeucauxuly,ktuser) values('" & mnode(j) & "','" & IDFlow & "','" & mnxl(j) & "','"& myc(j) &"','0')"
			conn.Execute sqll
		next
		conn.Close
		set conn=nothing	
	else
		bThongbao="Luong mau dai qua khong the gep duoc"
	end if		
	case else
	'*********Gui theo ca nhan*********	
		bTemp=nodeid
		if CheckNumberNode(bTemp,99,1) then 'Kiem tra so luong nhanh trong mot node=99
			sql1="Insert Into TvFlowNode(Maworkflownode,Maworkflow,nguoixuly,yeucauxuly,hanxuly,ykien,ktuser) values('" & nodeid & "','" & IDFlow & "','" & userid & "','"& Yeucau &"','"& thoihan &"','"& ykien &"','1')"
			conn.Execute sql1
			bThongbao=false
		else
			bThongbao=true
		end if
	conn.Close	
	set conn=nothing
	end select
	if request("xulyflow")="ok" then
		'lnk="../Editxulyflow.asp?bThongbao="& bThongbao &"&bMaRoot="& bMaRoot &"&IDFlow="& IDFlow &"&Nguoitao="& Nguoitao &"&Filename="& Filename
		lnk="../frmTvflowNode.asp?bThongbao="& bThongbao &"&IDFlow="& IDFlow
	else
		'lnk="../flow.asp?bThongbao="& bThongbao &"&bAdd=ok&bMaRoot="& bMaRoot &"&IDFlow="& IDFlow &"&Nguoitao="& Nguoitao &"&Filename="& Filename
		lnk="../frmTvflowNode.asp?bThongbao="& bThongbao &"&bAdd=ok&IDFlow="& IDFlow
	end if
	Response.Redirect(lnk)	
%>
<!--#include file="..\..\jvfunction.inc"-->
<script language=VBScript runat=server>
	'Vi tri bit so sanh
Function ItemLast(Item)
        Dim i
        Dim It
        Dim Vt
        Dim j
        It = Item
        It = Right(It, Len(It) - 4)
        j = 1
        For i = 2 To Len(It)
            If i < Len(It) Then
                Temp = Mid(It, i, 1)
                i = i + 1
                Temp = Temp & Mid(It, i, 1)
                j = j + 1
                If Temp = "00" Then
                    ItemLast = j - 1
                    Exit For
                Else
                    If i = Len(It) Then
                        ItemLast = j
                    End If
                End If
            End If
        Next
    End Function
    Function CheckNumberNode(nodeid,Max,num)
		dim Vt,bcat
		CheckNumberNode=true
		Vt=ITemLast(nodeid)		
		'5 la maworkflow,3 so chu so toi da cua mot node
		bcat=left(nodeid,5+(Vt-2)*2)& "%%%"		
		for i=Vt to 9
			bcat=bcat & "00"
		Next
		sSQL="Select max(maworkflownode) as bmax from flownode where maworkflownode like '%"& bCat &"%'"
		Set RecPM =server.CreateObject("ADODB.Recordset")
		RecPM.CursorType=3
		RecPM.CacheSize=5
		RecPM.Open sSQL,Conn		
		if not isnull(RecPM("bMax")) then
			bMax=RecPM("bMax")
			bMax=cint(right(left(bMax,5+(ItemLast(bMax)-1)*2),2))
			RecPM.Close
			Set RecPM=nothing
			if (bMax+num)>Max then
				CheckNumberNode=false
			end if		
		end if
	End Function
	Function Repl(Item, pos)
        Dim It
        Dim vt
        Dim bL
        It = Item
        bL = Left(It, 4)
        It = Right(It, Len(It) - 4)
        c = Len(It)
        It = Left(It, pos * 2 - 3)
        Repl = bL & It
    End Function
    Function GepNode(ItemS,ItemD)
		strcuts=repl(ItemS,ItemLast(ItemS)+1)
		strsocuoi=right(strcuts,2)
		
		strcutd=right(ItemD,18)
		strsodau=left(strcutd,2)
		
		inttang=cint(strsocuoi)+cint(strsodau)-1
		if inttang<=99 then
			if inttang < 10 then
				strtang="0"+cstr(inttang)
			else
				strtang=cstr(inttang)
			end if
		end if
		GepNode=repl(ItemS,ItemLast(ItemS))+strtang+right(ItemD,16)
		'GepNode=left(strnodephu,23)
    End Function
    Function CheckGepNode(ItemS,strMaworkflow)
		Check=true
		sSQL="Select maworkflownode from tvflownode Where maworkflow like '"& strMaworkflow &"' order by maworkflownode"
		Set RecCkLuong=server.CreateObject("ADODB.Recordset")
		RecCkLuong.CursorType=3
		RecCkLuong.CacheSize=5
		RecCkLuong.Open sSQL,Conn
		RecCkLuong.MoveFirst
		do while not RecCkLuong.EOF and Check=true
			strcknode=mid(GepNode(ItemS,RecCkLuong("maworkflownode")),24,2)
			if strcknode <> "00" then
				Check=true
			else 
				Check=false
			end if
		RecCkLuong.MoveNext
		loop
		RecCkLuong.Close
		Set RecCkLuong=nothing
		CheckGepNode=Check
    End function
	function CheckGepNode(ItemS,MaWorkFlowD)
		strnodeS=repl(nodeid,ItemLast(nodeid)+1)
		intlennodeS=len(strnodeS)
		intlennodeD=0
		sSQL="Select maworkflownode from tvflownode Where maworkflow like '"& MaWorkFlowD &"' order by maworkflownode"
		Set RecCheckLuongD=server.CreateObject("ADODB.Recordset")
		RecCheckLuongD.CursorType=3
		RecCheckLuongD.CacheSize=5
		RecCheckLuongD.Open sSQL,Conn
		RecCheckLuongD.MoveFirst
		do while not RecCheckLuongD.EOF
		i=len(repl(RecCheckLuongD("maworkflownode"),ItemLast(RecCheckLuongD("maworkflownode"))+1))-7
		if intlennodeD < i then
			intlennodeD=i
		end if
		RecCheckLuongD.MoveNext
		loop
		if intlennodeS+intlennodeD <= 23 then
			CheckGepNode=true
		else
			CheckGepNode=false
		end if
	end Function
</script>